package com.rjj.wlqq.tool.io;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

/**
 * @author r
 * @date 2020/12/10 16:56
 * 通道下载文件
 */
public class DownloadKit {

    /**
     * 下载文件到指定到文件夹
     *
     * @param urlString   请求地址
     * @param destination 下载到到地址 new File(pathName)
     * @throws Exception 异常
     */
    public static void downloadFileFromURL(String urlString, File destination) throws Exception {
        URL website = new URL(urlString);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        try (FileOutputStream fos = new FileOutputStream(destination)) {
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
            rbc.close();
        }
    }
}
